package com.panopset.gui.container;

import static java.awt.Color.RED;

import java.awt.Component;
import java.awt.LayoutManager;

import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import com.panopset.gui.LookAndFeel;
import com.panopset.gui.TPanel;
import com.panopset.gui.UtilGUI;
import com.panopset.swing.App;
import com.panopset.swing.FontManager;

/**
 * Titled panel.
 *
 * @author Karl Dinwiddie
 *
 */
public class TitledPanel {


    /**
     * Panel.
     */
    private TPanel panel;

    /**
     * @return TitledPanel's panel.
     */
    public final TPanel getPanel() {
        if (panel == null) {
            panel = new TPanel();
        }
        return panel;
    }

    /**
     * Border title.
     */
    private final String name;

    /**
     * Set normal highlighting.
     */
    public final void setNormalHighlighting() {
        LineBorder lb = new LineBorder(
                LookAndFeel.Colors.BORDER_COLOR_NORMAL.getColor(), 1);
        TitledBorder b = new TitledBorder(lb, name);
        if (App.app == null) {
            b.setTitleFont(FontManager.getFont(FontManager.FontType.borderTitle));
        } else {
            FontManager.register(b, FontManager.FontType.borderTitle);
        }
        b.setTitleColor(LookAndFeel.Colors.FG_COLOR.getColor());
        getPanel().setBorder(b);
        UtilGUI.updateComponent(getPanel());
    }

    /**
     * Set alert highlighting.
     */
    public final void setAlertHighlighting() {
        LineBorder lb = new LineBorder(RED, 1);
        TitledBorder b = new TitledBorder(lb, name);
        FontManager.register(b, FontManager.FontType.borderTitle);
        b.setTitleColor(RED);
        getPanel().setBorder(b);
        UtilGUI.updateComponent(getPanel());
    }

    /**
     * @param title
     *            Title.
     * @param mgr
     *            Layout manager.
     * @param toolTipText
     *            Tool tip text.
     * @param comp
     *            Component that the border surrounds.
     */
    public TitledPanel(final String title, final LayoutManager mgr,
            final String toolTipText, final Component comp) {
        this(title, mgr, toolTipText);
        getPanel().add(comp);
    }

    /**
     * @param title
     *            Title.
     * @param mgr
     *            Layout manager.
     * @param toolTipText
     *            Tool tip text.
     */
    public TitledPanel(final String title, final LayoutManager mgr,
            final String toolTipText) {
        getPanel().setLayout(mgr);
        name = title;
        setNormalHighlighting();
        getPanel().setBackground(LookAndFeel.Colors.BG_COLOR.getColor());
        getPanel().setToolTipText(toolTipText);
    }

    /**
     * Set title text.
     * @param toolTipText Text to set title text to.
     */
    public void setTitleText(String toolTipText) {
        TitledBorder tb = (TitledBorder) getPanel().getBorder();
        tb.setTitle(toolTipText);
        getPanel().setToolTipText(toolTipText);
        getPanel().invalidate();
        if (App.app != null) {
            App.app.f.getFrame().validate();
        }
    }
}
